/*
 * Copyright 2025 arisgo@163.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.arisgo.cloud.auth.sys.service.impl;

import com.arisgo.cloud.auth.sys.entity.FunctionDetail;
import com.arisgo.cloud.auth.sys.mapper.FunctionDetailMapper;
import com.arisgo.cloud.auth.sys.service.FunctionDetailService;
import com.arisgo.cloud.auth.sys.vo.FunctionDetailVo;
import com.arisgo.cloud.auth.sys.vo.FunctionMenuVo;
import com.arisgo.cloud.common.utils.AssertUtil;
import com.arisgo.cloud.web.mybatis.service.CloudServiceImpl;
import com.arisgo.cloud.web.param.Param;
import com.arisgo.cloud.web.param.Search;
import com.arisgo.cloud.web.utils.TreeUtil;
import com.google.common.collect.Lists;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 功能详情
 *
 * @author Arisgo
 * @since 2024-06-14
 */
@Service
public class FunctionDetailServiceImpl extends CloudServiceImpl<FunctionDetailMapper, FunctionDetail, FunctionDetailVo> implements FunctionDetailService {

    @Override
    public FunctionDetail getInstanceModel() {
        return new FunctionDetail();
    }

    @Override
    public FunctionDetailVo getInstanceVo() {
        return new FunctionDetailVo();
    }

    @Override
    public List<FunctionMenuVo> treeFunctionDetails(Long functionId) {
        return TreeUtil.convertTree(mapper.treeFunctionDetails(functionId));
    }

    @Override
    @Transactional
    public Boolean save(Long functionId, List<Long> menuIds) {
        AssertUtil.notNull(menuIds, "参数异常！");
        mapper.deleteByQuery(Param.creator(FunctionDetail.FUNCTION_ID, functionId).buildWrapper());
        if (menuIds.isEmpty()) {
            return true;
        }
        List<FunctionDetail> details = Lists.newArrayList();
        for (Long menuId : menuIds) {
            FunctionDetail detail = new FunctionDetail();
            detail.setFunctionId(functionId);
            detail.setMenuId(menuId);
            details.add(detail);
        }
        return mapper.insertBatch(details) > 0;
    }

    @Override
    @Transactional
    public Boolean remove(Long functionId, List<Long> menuIds) {
        Param functionParam = Param.creator(FunctionDetail.FUNCTION_ID, functionId);
        Param menuParams = Param.creator(FunctionDetail.MENU_ID, "IN", menuIds);
        Search search = Search.creator(functionParam, menuParams);
        mapper.deleteByQuery(search.buildWrapper());
        return true;
    }
}
